package com.example.app.viewmodel

import android.app.Application
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import com.example.app.data.bean.BaseResponseData
import com.example.app.data.bean.UserInfo
import com.example.app.util.CacheUtil
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class MainViewModel(

        routeViewModel: RouteViewModel
) : ViewModel() {
    //键盘getBottom
    var _imePaddingBottomValue = MutableStateFlow<Dp>(0.dp)
    var imePaddingBottomValue = _imePaddingBottomValue.asStateFlow()
    fun flowUpdate_imePaddingBottomValue(value: Dp) {
        _imePaddingBottomValue.update {
            value
        }
    }
    //键盘最大getBottom
    var _imeMaxPaddingBottomValue = MutableStateFlow<Dp>(0.dp)
    var imeMaxPaddingBottomValue = _imeMaxPaddingBottomValue.asStateFlow()
    fun flowUpdate_imeMaxPaddingBottomValue(value: Dp) {
        _imeMaxPaddingBottomValue.update {
            value
        }
    }
    //收入备注
    var _userInfo = MutableStateFlow<UserInfo?>(null)
    var userInfo = _userInfo.asStateFlow()
    fun flowUpdate_userInfo(value: UserInfo) {
        _userInfo.update {
            value
        }
    }

    init {
        //CacheUtil.setUser(null)
        var userInfo = CacheUtil.getUser()
        //路由初始化
        if (userInfo == null || userInfo._id.isEmpty()) {
            routeViewModel.flowUpdate_curPage(MainPages.LOGIN)
        } else {
            flowUpdate_userInfo(userInfo)
            routeViewModel.flowUpdate_curPage(MainPages.HOME)
            routeViewModel.flowUpdate_curHomeChild(HomeChild.ACCOUNT_LIST)
        }
    }





    //全局loading
    var _showMyCircularProgressIndicator = MutableStateFlow<Boolean>(false)
    var showMyCircularProgressIndicator = _showMyCircularProgressIndicator.asStateFlow()
    fun flowUpdate_showMyCircularProgressIndicator(value: Boolean) {
        _showMyCircularProgressIndicator.update {
            value
        }
    }

    fun <T> requestHandler(
            resultDeferred: BaseResponseData<T>,
            sucess: () -> Unit = {},
            fail: () -> Unit = {},
            end: () -> Unit = {},
    ) {
        var result = resultDeferred
    }
}